// subPages/mine/address/address.js
const app = getApp()
var util =  require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  //获取地址数据
  getData:function(){
    var that = this
    util.getAjax(app.globalData.apiUrl +'/get_user_address',function(res){
      // console.log(res)
      if(res.data.length!=0){
        that.setData({
          addressList: res.data,
          isOne: '0'
        })
      }else{
        that.setData({
          addressList: [],
          isOne: '1'
        })
      }
    },{type:2,user_id: that.data.userId})
  },

  //设置默认
  defaultAddress:function(e){
    var that = this
    var id = e.currentTarget.dataset.id
    var list = that.data.addressList
    var status = e.currentTarget.dataset.status
    if(status == '1'){
      return
    }
    var oldId = ''
    for(var i in list){
      if(list[i].status == '1'){
        oldId = list[i].id
        break;
      }
    }
    util.getAjax(app.globalData.apiUrl +'/save_address_status',function(res){
      // console.log(res)
      if(res.data == '1'){
        util.showToast('设置成功','none',1000)
        that.getData()
      }
    },{one_id: oldId,two_id: id})
  },

  //删除
  delete:function(e){
    var that = this
    var id = e.currentTarget.dataset.id
    var status = e.currentTarget.dataset.status
    if(status == '1'){
      util.showModal('提示','默认收货地址不可删除')
      return
    }
    util.getAjax(app.globalData.apiUrl +'/delete_user_address',function(res){
      // console.log(res)
      if(res.data == '1'){
        that.getData()
      }
    },{id: id})
  },

  //编辑
  editor:function(e){
    var list = e.currentTarget.dataset.item
    wx.navigateTo({
      url: '../newAddress/newAddress?list=' + JSON.stringify(list),
    })
  },

  //新增地址
  newBtn:function(){
    if(app.globalData.userId){
      wx.navigateTo({
        url: '../newAddress/newAddress?isOne=' + this.data.isOne,
      })
    }else{
      util.showModelAll('提示','该功能需要授权登录后使用，请进行授权登录',function(){
        if (app.globalData.userInfo) {
          //已授权
          wx.redirectTo({
            url: '/pages/login/login'
          })
        }else{
          // 在没有 open-type=getUserInfo 版本的兼容处理
          if(app.globalData.isAuthorized){
            app.userInfoReadyCallback = res => {
              if (res) {
                //已授权
                wx.redirectTo({
                  url: '/pages/login/login'
                })
              }else{
                //未授权
                wx.navigateTo({
                  url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ false,
                })
              }
            }
          }else{
            wx.navigateTo({
              url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ false,
            })
          }
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(app.globalData.userId){
      this.setData({
        userId: app.globalData.userId
      })
      this.getData()
    }else{
      app.userIdCallback = userId => {
        if (userId != '') {
          this.setData({
            userId: userId
          });
          this.getData()
        }
      }
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})